package com.chat.view;

import com.chat.Local;
import com.chat.service.SearchFriend;
import com.chat.service.SearchGroup;
import com.chat.service.SendGroupApply;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.util.Callback;

import java.io.IOException;

/*
  添加群聊的Controller类
 */
public class AddGroupController {

    public TextField idTextField;
    public Button search;
    public Label tishi;
    public Button add;
    public ListView addlist;
    public static ObservableList<AddListData> addgroup= FXCollections.observableArrayList();
    public void initialize(){
        add.setDisable(true);
    }
    public void searchgroup(MouseEvent mouseEvent) {
        add.setDisable(true);
        addlist.getItems().clear();
        addgroup.clear();
        addlist.setItems(addgroup);
        tishi.setText(" ");
        SearchGroup sg=new SearchGroup();
        sg.searchgroup(idTextField.getText());
        addlist.setCellFactory(new Callback<ListView, ListCell>() {
            @Override
            public ListCell call(ListView param) {
                return new ListCell<AddListData>(){
                    @Override
                    protected void updateItem(AddListData item, boolean empty){
                        super.updateItem(item,empty);
                        if(!empty){
                            setStyle("-fx-background-color:transparent");

                            HBox hbox=new HBox();
                            FXMLLoader fxmlLoader=new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("addfriendlist.fxml"));
                            try {
                                hbox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            AddFriendListController friendListController=fxmlLoader.getController();
                            hbox.setPadding(new Insets(20,0,0,0));
                            hbox.setPrefHeight(50);
                            friendListController.setNickname(item.getNickname());
                            friendListController.setQianming(item.qianming);
                            Platform.runLater(()-> {
                                this.setGraphic(hbox);
                            });

                        }
                        else {
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });
    }

    public void AddGroup(MouseEvent mouseEvent) {
        SendGroupApply sga=new SendGroupApply();
        sga.sendgroupapply(idTextField.getText(), Local.user.getUsername());
    }
}
